export class SubscriptionObject { subscribedPropsDic: any = {}; public readyForUpdate: boolean = false; private handler: (() => void) | undefined; constructor( public value: any, private onSubscribe: ( prop: string | number | symbol, subcriptionInstance: SubscriptionObject, ) => void, private onDestroy: (subcriptionInstance: SubscriptionObject) => void, ) { const instance = this; const subscriptionProxyHandler: ProxyHandler = { get: function (target, name) { if (!instance.subscribedPropsDic[name]) instance.subscribeTo(name); return target[name]; }, }; this.value = new Proxy(value, subscriptionProxyHandler); } private subscribeTo = (prop: string | number | symbol) => { this.subscribedPropsDic[prop] = true; this.onSubscribe(prop, this); }; public destroy = () => { this.onDestroy(this); }; public setHandler(fn: () => void) { this.handler = fn; } public update() { if (this.handler) this.handler(); this.readyForUpdate = false; } }